var error_chk = document.getElementById('error_chk');
var default_text= "Please fill all the informations bellow";
var loadEmail_bool=true;
function checkName() {
	var name=document.getElementById('name').value;
	var condition= alphanumeric(name) && name.length <= 32;
	error_chk.innerHTML = (condition ||name.length==0 ? default_text : "<font color='red'>Please correct your Name! </font>");
	return condition && name.length > 0;
};

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
    return pattern.test(emailAddress);
};

function alphanumeric(inputtxt)
{ 
	var letters = /^[a-zA-Z]+$/;
	return (inputtxt.match(letters) ? true:false);
}

function checkEmail(){
	var email = document.getElementById('email').value;
	var condition = (isValidEmailAddress(email)) ;
	if (checkName()){
		error_chk.innerHTML=(condition || email.length==0? default_text: "<font color='red'>Please correct your Email!</font>" );	
	}
	return condition && email.length > 0;
}

function checkpassword(){
	var pass = document.getElementById('password').value;
	// var repass = document.getElementById('repassword').value;
	var condition = (pass.length>5 && pass.length<33);
	if (checkEmail()){
		error_chk.innerHTML = (condition ? default_text:"<font color='red'>Please correct your Password length!</font>");	
	}
	
	return condition;
	
}

function checkRePassword() {
	var pass = document.getElementById('password').value;
	var repass = document.getElementById('repassword').value;
	var condition = (pass===repass) && repass.length!=0;
	if (checkpassword()){
		error_chk.innerHTML = (condition && checkName ? default_text:"<font color='red'>Please correct your Password and RePassword!</font>");
	}
	return condition;
}

function button_event(){
	document.getElementById("button_submit").disabled = true && !(checkRePassword()&& checkName() && checkEmail()&&loadEmail_bool);
}

window.onload = function (){
	document.getElementById("button_submit").disabled = true;
	loadEmail();
}

function loadEmail()
        {
            $.ajax({
                // The link we are accessing.
                url: "ajax/register.php",
                    
                // The type of request.
                type: "get",
                    
                // The type of data that is getting returned.
                dataType: "html",

                success: function( strData ){
                	var array_email = eval(strData);
                	var email =  document.getElementById('email').value;
                	var name = document.getElementById('name').value;
                	loadEmail_bool=true;
                	// error_chk.innerHTML = array_email[0][];
                	for (var i=0;i<array_email.length;i++){
                		// alert(true && !(checkRePassword()&& checkName() && checkEmail() && loadEmail()));
                		if (name == array_email[i][0]){
							error_chk.innerHTML = "<font color='red'>Your Name is existed!</font>";                			
							loadEmail_bool=false;
                			break;
                		}
                		else if (email == array_email[i][1]){

                			error_chk.innerHTML = "<font color='red'>Your Email is existed!</font>";
                			loadEmail_bool=false;
                			break;
                		}
                		else {
                			error_chk.innerHTML = default_text;
                			loadEmail_bool=true;
                		}
                	}
                }
            });
        }